package design.graduation.novelsystem.controller;

import design.graduation.novelsystem.model.dto.ResultObj;
import design.graduation.novelsystem.model.vo.CrawlerBookVo;
import design.graduation.novelsystem.service.CrawlerBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author: 洪少尉
 * @Date: 2020/4/17 12:27
 * @Description: 爬取信息管理
 */
@RestController
@RequestMapping("crawlerBook")
public class CrawlerBookController {
	@Autowired
	private CrawlerBookService crawlerBookService;

	/**
	 * 全查询+条件查询
	 */
	@GetMapping("loadAllCrawlerBook")
	public Object loadAllCrawlerBook(CrawlerBookVo crawlerBookVo){
		return this.crawlerBookService.queryAllCrawlerBook(crawlerBookVo);
	}
	/**
	 * 收录
	 */
	@PostMapping("include")
	public ResultObj include(Integer id){
		if (this.crawlerBookService.includeBook(id)){
			return ResultObj.INCLUDE_SUCCESS;
		}
		return ResultObj.INCLUDE_ERROR;
	}
	/**
	 * 批量收入
	 */
	@PostMapping("batchInclude")
	public ResultObj include(Integer[] ids){
		if(null!=ids&&ids.length>0){
			List<Integer> idsList = new ArrayList<>(Arrays.asList(ids));
			for (Integer id : idsList) {
				this.crawlerBookService.includeBook(id);
			}
			return ResultObj.INCLUDE_SUCCESS;
		}
		return ResultObj.INCLUDE_ERROR;
	}
}
